package com.zqj.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zqj.project.common.annotation.PreventDuplicateSubmit;
import com.zqj.project.common.entity.R;
import com.zqj.project.common.request.PageParams;
import com.zqj.project.converter.PhoneConverter;
import com.zqj.project.dto.PhoneDTO;
import com.zqj.project.service.PhoneService;
import com.zqj.project.util.Validator;
import com.zqj.project.vo.PhoneVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @Description: PhoneController
 * @Author: 22001010704 Zhang Qijian
 * @CreateTime: 2024/12/8 19:38
 * @Version: 1.0
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/phone")
@Tag(name = "Phone API", description = "手机管理接口，支持增删改查以及分页功能")
@RequiredArgsConstructor
public class PhoneController {

    // 自动生成依赖注入的构造函数
    private final PhoneService phoneService;

    // 分页查询手机列表
    @Operation(summary = "分页查询手机列表", description = "根据分页参数查询手机信息")
    @PostMapping("/page")
    public R<IPage<PhoneVO>> queryPhonePage(@RequestBody @Validated @NotNull @Parameter(description = "分页参数及筛选条件") PageParams<PhoneDTO> pageParams) {
        PhoneDTO phoneDTO = pageParams.getModel();
        Validator.priceValidation(phoneDTO.getLowerPrice(), phoneDTO.getUpperPrice());
        return R.success(PhoneConverter.toPhoneVoPage(this.phoneService.page(PhoneConverter.toPhoneModel(phoneDTO), new Page<>(pageParams.getCurrent(), pageParams.getSize()))));
    }

    // 查询手机详情
    @Operation(summary = "查询手机详情", description = "根据手机ID查询具体手机的详细信息")
    @GetMapping("/info/{id}")
    public R<PhoneVO> info(@PathVariable @Parameter(description = "手机ID", required = true) Long id) {
        return R.success(PhoneConverter.toPhoneVo(this.phoneService.info(id)));
    }

    // 上架手机
    @Operation(summary = "上架手机", description = "上架一台新的手机")
    @PostMapping("/add")
    @PreventDuplicateSubmit
    public R<String> add(@RequestBody @Validated @Parameter(description = "要上架的手机信息") PhoneDTO phoneDto) {
        return Boolean.TRUE.equals(this.phoneService.add(PhoneConverter.toPhoneModel(phoneDto))) ? R.success("手机上架成功") : R.fail("手机上架失败");
    }

    // 修改手机
    @Operation(summary = "修改手机", description = "根据手机ID更新手机信息")
    @PutMapping("/edit")
    @PreventDuplicateSubmit
    public R<String> edit(@RequestBody @Validated @Parameter(description = "要修改的手机信息，需包含ID") PhoneDTO phoneDto) {
        if (Objects.isNull(phoneDto.getId())) {
            return R.fail("手机ID不能为空");
        }
        return Boolean.TRUE.equals(this.phoneService.edit(PhoneConverter.toPhoneModel(phoneDto))) ? R.success("手机修改成功") : R.fail("手机修改失败");
    }

    // 下架手机
    @Operation(summary = "下架手机", description = "根据手机ID下架手机信息")
    @DeleteMapping("/delete/{id}")
    @PreventDuplicateSubmit
    public R<String> delete(@PathVariable @Parameter(description = "手机ID", required = true) Long id) {
        return Boolean.TRUE.equals(this.phoneService.delete(id)) ? R.success("手机下架成功") : R.fail("手机下架失败");
    }

}